#include "UserManagerThreadSafe.h"
#include "User.h"
#include "Mutex.h"

IUserManager*	UserManagerThreadSafe::_instance = NULL;

UserManagerThreadSafe::UserManagerThreadSafe()
{
	//this->LoadUser(); pas utile on load pas tout les users, en memoire on a que ceux connectes
	this->_mtx = new Mutex();
}

IUserManager*	UserManagerThreadSafe::GetUserManager()
{
	if (UserManagerThreadSafe::_instance == NULL)
		UserManagerThreadSafe::_instance = new UserManagerThreadSafe();
	return UserManagerThreadSafe::_instance;
}

IUser*			UserManagerThreadSafe::GetUser(unsigned int id)
{
	this->_mtx->Lock();
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent)->GetId() == id)
		{
			this->_mtx->Unlock();
			return *iteCurrent;
		}
		++iteCurrent;
	}
	this->_mtx->Unlock();
	return NULL;
}

IUser*			UserManagerThreadSafe::GetUser(std::string& pattern)
{
	this->_mtx->Lock();
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent)->GetLogin() == pattern)
		{
			this->_mtx->Unlock();
			return *iteCurrent;
		}
		++iteCurrent;
	}
	this->_mtx->Unlock();
	return NULL;
}

unsigned int	UserManagerThreadSafe::LoadUser()
{
	this->_mtx->Lock();
	this->UnloadUser();
	//load depuis la base tout les users
	//this->_user.push_back(user);
	this->_mtx->Unlock();
	return 0;
}

unsigned int	UserManagerThreadSafe::LoadUser(unsigned int id)
{
	this->_mtx->Lock();
	this->UnloadUser(id);
	//load depuis la base le user 'id'
	//IUser*	user = new User(login, password);
	//this->_user.push_back(user);
	this->_mtx->Unlock();
	return 0;
}

unsigned int	UserManagerThreadSafe::LoadUser(std::string& pattern)
{
	this->_mtx->Lock();
	this->UnloadUser(pattern);
	//load depuis la base le user 'pattern'
	//IUser*	user = new User(login, password);
	//this->_user.push_back(user);
	this->_mtx->Unlock();
	return 0;
}

unsigned int	UserManagerThreadSafe::UnloadUser()
{
	this->_mtx->Lock();
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		//ecrire info DB
		++iteCurrent;
	}
	this->_user.clear();
	this->_mtx->Unlock();
	return 0;
}

unsigned int	UserManagerThreadSafe::UnloadUser(IUser* user)
{
	this->_mtx->Lock();
	//ecire info DB de ce user
	this->_user.remove(user);
	this->_mtx->Unlock();
	return 0;
}

unsigned int	UserManagerThreadSafe::UnloadUser(unsigned int id)
{
	this->_mtx->Lock();
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent)->GetId() == id)
		{
			//ecrire info DB
			this->_user.erase(iteCurrent);
			this->_mtx->Unlock();
			return 0;
		}
		++iteCurrent;
	}
	this->_mtx->Unlock();
	return -1;
}

unsigned int	UserManagerThreadSafe::UnloadUser(std::string& pattern)
{
	this->_mtx->Lock();
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent)->GetLogin() == pattern)
		{
			//ecrire info DB
			this->_user.erase(iteCurrent);
			this->_mtx->Unlock();
			return 0;
		}
		++iteCurrent;
	}
	this->_mtx->Unlock();
	return -1;
}

IUser*	UserManagerThreadSafe::CreateUser(const std::string& login, const std::string& password)
{
	//creer dans la base
	IUser *user = new User(login, password);
	this->_mtx->Lock();
	this->_user.push_back(user);
	this->_mtx->Unlock();
	return user;
}

unsigned int	UserManagerThreadSafe::DeleteUser()
{
	this->_mtx->Lock();
	//remove de la base
	//this->_user.remove(user);
	this->_mtx->Unlock();
	return 0;
}

unsigned int	UserManagerThreadSafe::UserExists(const std::string& login)
{
	return 0;
}
